package edu.scau.mis.pos.service.impl;

import edu.scau.mis.core.config.SnowFlakeUtil;
import edu.scau.mis.pos.domain.Payment;
import edu.scau.mis.pos.mapper.PaymentMapper;
import edu.scau.mis.pos.service.IPaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Payment)表服务实现类
 *
 * @author CJW
 * @since 2024-04-17 23:51:46
 */
@Service("paymentService")
public class PaymentServiceImpl implements IPaymentService {
    @Resource
    private PaymentMapper paymentMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param paymentId 主键
     * @return 实例对象
     */
    @Override
    public Payment selectById(Long paymentId) {
        return this.paymentMapper.selectById(paymentId);
    }

    /**
     * 查询指定行
     *
     * @param payment 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Payment> selectList(Payment payment) {
        return this.paymentMapper.selectList(payment);
    }

    /**
     * 新增数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    @Override
    public Payment insert(Payment payment) {
        payment.setCreateTime(new Date());
        payment.setPayTime(new Date());
        this.paymentMapper.insert(payment);
        return payment;
    }


    /**
     * 修改数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    @Override
    public Payment update(Payment payment) {
        payment.setUpdateTime(new Date());
        this.paymentMapper.update(payment);
        return this.selectById(payment.getPaymentId());
    }

    /**
     * 通过主键批量删除数据
     *
     * @param paymentId 主键
     * @return 影响记录数
     */
    @Override
    public int deleteByIds(Long paymentId) {
        return this.paymentMapper.deleteByIds(paymentId);
    }

    @Override
    public int createPayment(Payment payment) {
        Payment payment1 = new Payment();
        payment1.setOrderId(payment.getOrderId());
        List<Payment> selectPayment = paymentMapper.selectList(payment1);
        if (!selectPayment.isEmpty()) {
            return 0;
        } else {
            // 利用雪花算法生成payment_no
            String paymentNo = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
            payment.setPaymentNo(paymentNo);
            payment.setPayTime(new Date());//插入支付时间
            payment.setCreateTime(new Date());
            // 返回插入后的支付记录
            return paymentMapper.insert(payment);
        }
    }
    @Override
    public List<Map<String, Object>> getSalesByMonthAndYear(int year) {
        return this.paymentMapper.getSalesByMonthAndYear(year);
    }

    @Override
    public List<Map<String, Object>> getStylistPerformanceByTimeUnit(String timeUnit, String searchTime) {
        return paymentMapper.getStylistPerformanceByTime(timeUnit,searchTime);
    }
}
